#include <TextStream.hpp>

Modeling::TextStream::~TextStream()
{}

Modeling::TextStream::TextStream(std::string name, std::string mode) : DiskStream(name, mode)
{}

size_t Modeling::TextStream::read(int* value)
{
	return fscanf(file, "%d\n", value);
}

size_t Modeling::TextStream::read(unsigned int* value)
{
	return fscanf(file, "%u\n", value);
}

size_t Modeling::TextStream::read(float* value)
{
	return fscanf(file, "%f\n", value);
}

size_t Modeling::TextStream::read(std::string* str)
{
	char lchar;
	do {
		fread(&lchar, sizeof(char), 1, file);
		if ((lchar != '\n') && (lchar != '\0')) str->push_back(lchar);
	}while ((lchar != '\n') && (lchar != '\0'));

	return 0;
}

size_t Modeling::TextStream::read(Vector3ui* vector)
{
	unsigned int* c = (unsigned int*)vector;
	return fscanf(file, "(%u, %u, %u)\n", &c[0], &c[1], &c[2]);
}

size_t Modeling::TextStream::read(Vector3f* vector)
{
	float* c = (float*)vector;
	return fscanf(file, "(%f, %f, %f)\n", &c[0], &c[1], &c[2]);
}

size_t Modeling::TextStream::read(Vector2f* vector)
{
	float* c = (float*)vector;
	return fscanf(file, "(%f, %f)\n", &c[0], &c[1]);
}

size_t Modeling::TextStream::read(Modeling::RGBf* rgb)
{
	float* c = (float*)rgb;
	return fscanf(file, "(%f, %f, %f)\n", &c[0], &c[1], &c[2]);
}

size_t Modeling::TextStream::read(Matrix4f* matrix)
{
	size_t bytes = 0;
	bytes += fscanf(file, "[%f, %f, %f, %f]\n", &((*matrix)[0][0]), &((*matrix)[0][1]), &((*matrix)[0][2]), &((*matrix)[0][3]));
	bytes += fscanf(file, "[%f, %f, %f, %f]\n", &((*matrix)[1][0]), &((*matrix)[1][1]), &((*matrix)[1][2]), &((*matrix)[1][3]));
	bytes += fscanf(file, "[%f, %f, %f, %f]\n", &((*matrix)[2][0]), &((*matrix)[2][1]), &((*matrix)[2][2]), &((*matrix)[2][3]));
	bytes += fscanf(file, "[%f, %f, %f, %f]\n", &((*matrix)[3][0]), &((*matrix)[3][1]), &((*matrix)[3][2]), &((*matrix)[3][3]));
	return bytes;
}

size_t Modeling::TextStream::write(int value)
{
	return fprintf(file, "%d\n", value);
}

size_t Modeling::TextStream::write(unsigned int value)
{
	return fprintf(file, "%u\n", value);
}

size_t Modeling::TextStream::write(float value)
{
	return fprintf(file, "%f\n", value);
}

size_t Modeling::TextStream::write(std::string str)
{
	return fprintf(file, "%s\n", str.c_str());
}

size_t Modeling::TextStream::write(Vector3ui vector)
{
	return fprintf(file, "(%u, %u, %u)\n", vector[0], vector[1], vector[2]);
}

size_t Modeling::TextStream::write(Vector3f vector)
{
	return fprintf(file, "(%f, %f, %f)\n", vector[0], vector[1], vector[2]);
}

size_t Modeling::TextStream::write(Vector2f vector)
{
	return fprintf(file, "(%f, %f)\n", vector[0], vector[1]);
}

size_t Modeling::TextStream::write(Modeling::RGBf rgb)
{
	return fprintf(file, "(%f, %f, %f)\n", rgb.red(), rgb.green(), rgb.blue());
}

size_t Modeling::TextStream::write(Matrix4f matrix)
{
	size_t bytes = 0;
	bytes += fprintf(file, "[%f, %f, %f, %f]\n", matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3]);
	bytes += fprintf(file, "[%f, %f, %f, %f]\n", matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3]);
	bytes += fprintf(file, "[%f, %f, %f, %f]\n", matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3]);
	bytes += fprintf(file, "[%f, %f, %f, %f]\n", matrix[3][0], matrix[3][1], matrix[3][2], matrix[3][3]);
	return bytes;
}
